package com.iweb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iweb.annotation.TokenAuthentication;
import com.iweb.common.page.DataGrid;
import com.iweb.common.result.ResultEntity;
import com.iweb.model.Notice;
import com.iweb.service.NoticeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 通知 前端控制器
 * </p>
 *
 * @author jack
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @TokenAuthentication
    @ApiOperation("添加新消息")
    @PostMapping
    public ResultEntity<Notice> addNotice(@RequestBody Notice notice) {
        // 设置消息通知时间
        notice.setCreateTime(LocalDateTime.now());
        // 设置消息通知状态
        notice.setState("0");// 0 表示未读 1 表示已读
        // 保存消息通知
        noticeService.save(notice);
        return ResultEntity.SUCCESS(notice);
    }

    @TokenAuthentication
    @ApiOperation("根据消息类型查询当前用户的消息")
    @GetMapping("findNoticeByPage/{type}/{current}/{size}")
    public ResultEntity<DataGrid<Notice>> findNoticeByPage(@PathVariable String type,
                                                           @PathVariable Integer current,
                                                           @PathVariable Integer size) {
        // 设置分页条件
        Page<Notice> page = new Page<>(current, size);
        // 设置查询条件
        // 获取在线用户
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String revId = String.valueOf(request.getAttribute("userId"));
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        queryWrapper.eq("receiver_id", revId);
        // 查询
        IPage<Notice> noticeIPage = noticeService.page(page, queryWrapper);
        // 封装返回数据
        DataGrid<Notice> dataGrid = new DataGrid<>();
        dataGrid.setTotal(noticeIPage.getTotal());
        dataGrid.setRows(noticeIPage.getRecords());
        return ResultEntity.SUCCESS(dataGrid);
    }

    @TokenAuthentication
    @ApiOperation("根据通知ID查询通知")
    @GetMapping("{noticeId}")
    public ResultEntity<Notice> findById(@PathVariable String noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return ResultEntity.SUCCESS(notice);
    }

    @TokenAuthentication
    @ApiOperation("修改通知状态")
    @PutMapping("{noticeId}")
    public ResultEntity<String> updateNoticeStat(@PathVariable String noticeId) {
        Notice notice = new Notice();
        notice.setId(noticeId);
        notice.setState("1");
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return ResultEntity.SUCCESS("修改成功");
        } else {
            return ResultEntity.FAIL("修改失败");
        }
    }
}

